using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.SupplyChain.Material;
using HengTong.Model;


namespace HengTong.ViewModel.SupplyChain.SysMaterialSkuVms
{
    public partial class SysMaterialSkuListVm : BaseListVm<SysMaterialSkuView, SysMaterialSkuSearcher>
    {

        protected override IEnumerable<IGridColumn<SysMaterialSkuView>> InitGridHeader()
        {
            return new List<GridColumn<SysMaterialSkuView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.ErpId),
                this.MakeGridHeader(x => x.ErpName),
                this.MakeGridHeader(x => x.ErpModel),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.State),
                this.MakeGridHeader(x => x.BarCode),
                this.MakeGridHeader(x => x.TaxPrice),
                this.MakeGridHeader(x => x.Price),
                this.MakeGridHeader(x => x.LastInStockTime),
                this.MakeGridHeader(x => x.UnitName),
                this.MakeGridHeader(x => x.MaterialName),
                this.MakeGridHeader(x => x.AttributesName),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysMaterialSkuView> GetSearchQuery()
        {
            var query = DC.Set<SysMaterialSku>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckContain(Searcher.ErpId, x=>x.ErpId)
                .CheckContain(Searcher.ErpName, x=>x.ErpName)
                .CheckContain(Searcher.ErpModel, x=>x.ErpModel) 
                .CheckEqual(Searcher.State, x=>x.State)
                .CheckBetween(Searcher.LastInStockTime?.GetStartTime(), Searcher.LastInStockTime?.GetEndTime(), x => x.LastInStockTime, includeMax: false)
                .CheckEqual(Searcher.MaterialId, x=>x.MaterialId)
                .Select(x => new SysMaterialSkuView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Code = x.Code,
                    ErpId = x.ErpId,
                    ErpName = x.ErpName,
                    ErpModel = x.ErpModel,
                    Description = x.Description,
                    State = x.State,
                    BarCode = x.BarCode,
                    TaxPrice = x.TaxPrice,
                    Price = x.Price,
                    LastInStockTime = x.LastInStockTime,
                    UnitName = x.Unit.Name,
                    MaterialName = x.Material.Name,
                    AttributesName = x.Attributes.Select(y=>y.Value.Value).ToSepratedString(null,","), 
                    Remark = x.Remark,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysMaterialSkuView : SysMaterialSku{
        [Display(Name = "名称")]
        public String UnitName { get; set; }
        [Display(Name = "名称")]
        public String MaterialName { get; set; }
        public String AttributesName { get; set; }

    }
}
